Pagination এবং Sorting এমন দুটি গুরুত্বপূর্ণ ফিচার যা বড় ডাটাবেজ ডেটা সেট পরিচালনা করতে ব্যবহৃত হয়। Pagination ডেটাকে ছোট ছোট অংশে ভাগ করে প্রদর্শন করে, এবং Sorting ডেটা সাজানোর জন্য ব্যবহৃত হয়। Spring Data JPA এর মাধ্যমে এগুলো সহজেই বাস্তবায়ন করা যায়।
Spring Boot ব্যবহার করে Pagination এবং Sorting ইমপ্লিমেন্ট করার জন্য নিম্নলিখিত স্টেপগুলো অনুসরণ করুন।
pom.xml
ডিপেন্ডেন্সিSpring Boot এবং JPA এর ডিপেন্ডেন্সি যোগ করুন।
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
package com.example.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Spring Data JPA এর মাধ্যমে PagingAndSortingRepository
অথবা JpaRepository
ব্যবহার করা যায়।
package com.example.demo.repository;
import com.example.demo.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
Pagination এবং Sorting অপারেশন সার্ভিস লেয়ারে পরিচালনা করুন।
package com.example.demo.service;
import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Page<Product> getProducts(int page, int size, String sortBy, String direction) {
Sort sort = direction.equalsIgnoreCase("asc") ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending();
Pageable pageable = PageRequest.of(page, size, sort);
return productRepository.findAll(pageable);
}
}
Pagination এবং Sorting এর জন্য REST API তৈরি করুন।
package com.example.demo.controller;
import com.example.demo.entity.Product;
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/products")
public Page<Product> getProducts(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(defaultValue = "name") String sortBy,
@RequestParam(defaultValue = "asc") String direction
) {
return productService.getProducts(page, size, sortBy, direction);
}
}
Spring Boot এর CommandLineRunner
ব্যবহার করে কিছু টেস্ট ডেটা ইনসার্ট করুন।
package com.example.demo;
import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class DataLoader implements CommandLineRunner {
@Autowired
private ProductRepository productRepository;
@Override
public void run(String... args) throws Exception {
productRepository.save(new Product("Laptop", 800.00));
productRepository.save(new Product("Smartphone", 500.00));
productRepository.save(new Product("Tablet", 300.00));
productRepository.save(new Product("Smartwatch", 150.00));
}
}
URL:http://localhost:8080/products?page=0&size=2
Output:
{
"content": [
{
"id": 1,
"name": "Laptop",
"price": 800.0
},
{
"id": 2,
"name": "Smartphone",
"price": 500.0
}
],
"pageable": {
"pageNumber": 0,
"pageSize": 2,
...
},
...
}
URL:http://localhost:8080/products?page=0&size=10&sortBy=price&direction=desc
Output:
{
"content": [
{
"id": 1,
"name": "Laptop",
"price": 800.0
},
{
"id": 2,
"name": "Smartphone",
"price": 500.0
},
...
]
}
Spring ORM এবং JPA-র সাহায্যে Pagination এবং Sorting দ্রুত এবং সহজে ইমপ্লিমেন্ট করা যায়। Pageable
এবং Sort
ক্লাস ব্যবহার করে ডেটাকে ছোট অংশে ভাগ করা এবং নির্দিষ্ট ক্রমে সাজানো সম্ভব। এই পদ্ধতি বড় ডেটাসেট পরিচালনার জন্য খুবই কার্যকর।
Read more